<?php 
define("DIR","C:/upload_file");//上传文件的暂存位置路径
 function getRealSize($size)
 { 
  $kb = 1024;   // Kilobyte
  $mb = 1024 * $kb; // Megabyte
  $gb = 1024 * $mb; // Gigabyte
  $tb = 1024 * $gb; // Terabyte
  if($size < $kb)
  { 
   return $size." B";
  }
  else if($size < $mb)
  { 
   return round($size/$kb,2)." KB";
  }
  else if($size < $gb)
  { 
   return round($size/$mb,2)." MB";
  }
  else if($size < $tb)
  { 
   return round($size/$gb,2)." GB";
  }
  else
  { 
   return round($size/$tb,2)." TB";
  }
 }
if(isset($_GET['param'])){$param=htmlspecialchars($_GET['param']);}else{exit("false");}
$connect=mysqli_connect("localhost","root","root","qrcode",3306) ;
if(isset($_GET['insert_code'])){
	$context="insert into qrcode(code_context,code_status,code_outtime) values('{$param}','0','0')";
	$result=mysqli_query($connect,$context);
	if($result){echo "已插入数据";}else{echo "数据插入失败";}
	exit();
}
if(isset($_GET['auth_code'])){
	$context="select * from qrcode where `code_context`='{$param}'";
	$result=mysqli_query($connect,$context);
	$array=mysqli_fetch_array($result);
	if($array['code_status']==0){echo "0";}else{
		if(!is_dir(DIR."/t_file/".$param)){
		mkdir(DIR."/t_file/".$param);
	}
		echo "1";}
	exit();
}
if(isset($_GET['code_active'])){
	$outtime=time()+3600;
	$context="update qrcode set code_status='1' , `code_outtime`='{$outtime}'where code_context='{$param}'";
	$result=mysqli_query($connect,$context);
	if($result){
		setcookie("code",$param,time()+30);//激活成功
	}else{
		setcookie("code",null,time()+30);//未知原因的激活失败
	}
	header("Location:index.html");
	exit();
}
if(isset($_POST['del_code'])){
	$context="DELETE FROM `qrcode` WHERE `code_context` = '{$param}' LIMIT 1;";
	$result=mysqli_query($connect,$context);
	if($result){
		echo "激活码已注销";
	}else{
		echo "激活码注销失败";
	}
	exit();
}
if(isset($_POST['show_file_list'])){
	if(filesize(DIR."/t_file/".$param."/")>31457280){
		echo "<div id='file_list'><tr1><td2>-</td3><td2>超出可用空间,请删除部分文件后再上传</td3><td2>-</td3></tr4></div>";
		exit();
	}
	$dir_list=scandir(DIR."/t_file/".$param."/");
	$dir_list=array_splice($dir_list,2);
	if(count($dir_list)>0){
	echo "<div id='file_list'>";
	foreach($dir_list as $dl){
		$dl=iconv("gbk", "utf-8",$dl);
		$filesize="0KB";
		if(file_exists(DIR."/t_file/".$param."/".$dl)!=0){
		$filesize=getRealSize(filesize(DIR."/t_file/".$param."/".$dl));
		}
		echo "<tr1><td2>".$dl."</td3><td2>".$filesize."</th3><td2><button type='button' onclick=\"window.open('index.php?param=".$param."&download_file=".$dl."');\">下载</button><button type='button' onclick=\"del_file('".$param."','".$dl."')\">删除</button></td3></tr4>";
	}
	echo "</div>";
}else{
	echo "<div id='file_list'><tr1><td2>-</td3><td2>暂无文件数据</td3><td2>-</td3></tr4></div>";
}
	exit();
	}
	if(isset($_GET['file_space'])){
		echo "剩余可用空间:".getRealSize(31457280-filesize(DIR."/t_file/".$param."/"));
		exit();
	}
if(isset($_POST['del_file'])){
	$filename=htmlspecialchars($_POST['del_file']);
	if(unlink(DIR."/t_file/".$param."/".$filename)){
		echo "文件已删除";
	}else{
		echo "文件删除失败";
	}
		}
if(isset($_GET['download_file'])){
	$file_dir=DIR."/t_file/".$param."/";
	$file_name=htmlspecialchars($_GET['download_file']);
	//检查文件是否存在
    if (! file_exists ( $file_dir . $file_name )) {
		header('HTTP/1.1 404 NOT FOUND');
		} else { 
		//以只读和二进制模式打开文件   
		$file = fopen ( $file_dir . $file_name, "rb" ); 
		//告诉浏览器这是一个文件流格式的文件    
		Header ( "Content-type: application/octet-stream" ); 
		//请求范围的度量单位  Header ( "Accept-Ranges: bytes" ); 
		//Content-Length是指定包含于请求或响应中数据的字节长度    
		Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) ); 
		//用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。 
		Header ( "Content-Disposition: attachment; filename=" . $file_name ); 
		//读取文件内容并直接输出到浏览器    
		echo fread ( $file, filesize ( $file_dir . $file_name ) ); 
		fclose ( $file ); 
		exit (); 
}
}
if(isset($_GET['upload_file'])){
	$upFile = $_FILES['file'];
//判断文件是否为空或者出错
if ($upFile['error']==0 && !empty($upFile)) {
 $filename = $_FILES['file']['name'];
 $filename=iconv("gbk", "utf-8",$filename);
 $queryPath = DIR.'/t_file/'.$param.'/'.$filename;
 //move_uploaded_file将浏览器缓存file转移到服务器文件夹
 if(move_uploaded_file($_FILES['file']['tmp_name'],$queryPath)){
  echo "上传成功";
 }else{
	 echo "上传失败";
 }
}
}
?>